package com.lxl.hadoop.table;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;

import java.io.IOException;

/**
 * @Author lixilin
 * @Date 2022/1/2 21:29
 * @Description
 */
public class TableMapper extends Mapper<LongWritable, Text, Text, TableBean> {

    String name;

    @Override
    protected void setup(Context context) throws IOException, InterruptedException {
        // 获取切片信息
        FileSplit inputSplit = (FileSplit) context.getInputSplit();
        // 获取文件名称
        name = inputSplit.getPath().getName();
    }

    TableBean bean = new TableBean();
    Text k = new Text();
    String order = "order";
    String pd = "pd";
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String[] words = value.toString().split("\t");
        if (name.startsWith(order)) {
            bean.setOrderId(words[0]);
            bean.setPid(words[1]);
            bean.setAmount(Integer.parseInt(words[2]));
            bean.setPname("");
            bean.setFlag(order);

            k.set(words[1]);
        } else {
            bean.setOrderId("");
            bean.setPid(words[0]);
            bean.setAmount(0);
            bean.setPname(words[1]);
            bean.setFlag(pd);

            k.set(words[0]);
        }
        context.write(k,bean);
    }
}
